//
//  TopDropDownBtnView.m
//  WeCommunity
//
//  Created by mac on 2017/8/10.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "TopDropDownBtnView.h"

@interface TopDropDownBtnView ()

@property (nonatomic,strong) UIButton * contentButton;

@property (nonatomic,strong) UIImageView * arrowView;

@end

@implementation TopDropDownBtnView

-(UIButton *)contentButton
{
    if (!_contentButton) {
        _contentButton = [[UIButton alloc] initWithFrame:self.bounds];
        [_contentButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        _contentButton.titleLabel.font = PFM_FONT(17);
        [_contentButton addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
        _contentButton.exclusiveTouch = YES;
    }
    return _contentButton;
}

-(UIImageView *)arrowView
{
    if (!_arrowView) {
        _arrowView = [[UIImageView alloc] init];
        _arrowView.image = [UIImage imageNamed:@"arrow_down"];
        _arrowView.userInteractionEnabled = NO;
    }
    
    return _arrowView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor  = [UIColor colorWithWhite:1.0 alpha:0.0];
        
        [self addSubview:self.contentButton];
        
        [self.contentButton addSubview:self.arrowView];
    }
    return self;
}

-(void)setContentValue:(NSString *)contentValue
{
    _contentValue = contentValue;
    
    [self.contentButton setTitle:contentValue forState:UIControlStateNormal];
    
    [self setupArrowViewFrame];
    
    self.contentButton.selected = YES;
    [self processBtn:self.contentButton];
}

-(void)setupArrowViewFrame
{
    NSDictionary * attriDic = @{NSFontAttributeName:self.contentButton.titleLabel.font};
    CGSize size = [self.contentValue boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                                                     options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                                  attributes:attriDic
                                                                     context:nil].size;
    self.arrowView.frame = CGRectMake(CGRectGetMidX(self.contentButton.bounds) + size.width * 0.5 + 6, 18, 8, 8);
    
}

-(void)processBtn:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    [self arrowAnimationWithUp:sender.selected animation:YES];
    
    if ([self.delegate respondsToSelector:@selector(dropDownBtnSelected:)]) {
        [self.delegate dropDownBtnSelected:sender.selected];
    }
    
}
-(void)arrowAnimationWithUp:(BOOL)up animation:(BOOL)animation
{
    NSTimeInterval animationTime = 0;
    if (animation) {
        animationTime = KAnimationDuration;
    }
    if (up) {
        [UIView animateWithDuration:animationTime animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(M_PI);
        } completion:^(BOOL finished) {
            
        }];
    }else{
        [UIView animateWithDuration:animationTime animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(0);
        } completion:^(BOOL finished) {
            
        }];
    }
}


@end
